package com.atgouwu.mall.order.interceptor;

import com.atgouwu.mall.common.utils.JwtUtils;
import com.atgouwu.mall.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
@Component
public class MyHandlerInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

//    //目标方法执行前:校验登录
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String uri = request.getRequestURI();
//        AntPathMatcher antPathMatcher = new AntPathMatcher();
//        boolean match = antPathMatcher.match("/order/order/status/**", uri);
//        boolean match1 = antPathMatcher.match("/payed/notify", uri);
//        if (match || match1) {
//            return true;
//        }
//        MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(LOGIN_USER); //获取登录用户信息
//        if (attribute != null) {
//            loginUser.set(attribute); //登录用户信息放在ThreadLocal里保存
//            return true;
//        } else {
//            response.setContentType("text/html;charset=UTF-8"); //未登录返回登录页面
//            PrintWriter out = response.getWriter();
//            out.println("<script>alert('请先进行登录，再进行后续操作！');location.href='http://auth.gulimall.com/login.html'</script>");
//            // session.setAttribute("msg", "请先进行登录");
//            // response.sendRedirect("http://auth.gulimall.com/login.html");
//            return false;
//        }
//    }

    //目标方法执行前:校验登录
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher(); //Spring路径匹配器(支持带通配符)
        boolean match = antPathMatcher.match("/payed/notify", uri); //支付宝异步回调,不需验证登录
        if (match) {
            return true;
        }
        MemberResponseVo attribute = new MemberResponseVo();
        String token = request.getHeader("token");
        if (JwtUtils.checkToken(token)) { //已登录
            attribute.setId(JwtUtils.getUserId(token));
            attribute.setUsername(JwtUtils.getUserName(token));
            attribute.setToken(token);
            loginUser.set(attribute); //登录用户存入ThreadLocal
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
